/* Copyright (c) 2004-2024 peigen.info. All rights reserved. */

package info.peigen.hotpot.business.account.facade.enums;

import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson2.annotation.JSONCreator;
import com.alibaba.fastjson2.annotation.JSONType;
import com.google.common.collect.Lists;
import info.peigen.hotpot.common.core.enums.Messageable;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;

import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * <b>(AccountTransCode)</b>
 * 账户交易代码
 *
 * @author Peigen
 * @version 1.0.0
 * @since 2021/8/12
 */
@Getter
@NoArgsConstructor
@AllArgsConstructor
@JSONType(writeEnumAsJavaBean = true)
public enum AccountTransCode implements Messageable {
    /** 为了对抗 Alibaba Java Coding 而存在的注释 */
    Income("Income", "收入"),
    Paid("Paid", "支出"),
    PaidRefund("PaidRefund", "支出退款"),
    Deposit("Deposit", "充值"),
    Withdraw("Withdraw", "提现"),
    Transfer("Transfer", "转账"),
    Settlement("Settlement", "结算"),
    Fee("Fee", "收费"),
    /** 不晓得啷格回事 */
    UNKNOWN("--", "不晓得啷格回事"),
    ;

    /** 枚举值 */
    String code;

    /** 枚举描述 */
    String message;

    /**
     * 通过枚举<code>code</code>获得枚举
     *
     * @param code 枚举值
     *
     * @return AccountTransCode
     */
    public static AccountTransCode getByCode(String code) {
        return Stream.of(values())
                .filter(innerEnum -> StrUtil.equals(code, innerEnum.code()))
                .findFirst().orElse(UNKNOWN);
    }

    /**
     * 通过枚举<code>name</code>获得枚举
     *
     * @param name 枚举名
     *
     * @return AccountTransCode
     */
    public static AccountTransCode getByName(String name) {
        return Stream.of(values())
                .filter(innerEnum -> StrUtil.equalsIgnoreCase(name, innerEnum.name()))
                .findFirst().orElse(UNKNOWN);
    }

    @JSONCreator
    public static AccountTransCode deserialize(String code) {
        return Stream.of(values())
                .filter(innerEnum -> StrUtil.equals(code, innerEnum.code()))
                .findFirst().orElse(UNKNOWN);
    }

    /**
     * 获取全部枚举
     *
     * @return List<AccountTransCode>
     */
    public static List<AccountTransCode> getAllEnum() {
        return Lists.newArrayList(values());
    }

    /**
     * 获取全部枚举值
     *
     * @return List<String>
     */
    public static List<String> getAllEnumCode() {
        return getAllEnum().stream().map(AccountTransCode::code).collect(Collectors.toList());
    }

    /**
     * 通过code获取msg
     *
     * @param code 枚举值
     *
     * @return String
     */
    public static String getMsgByCode(String code) {
        return getByCode(code).message();
    }

    /**
     * 获取枚举code
     *
     * @param innerEnum 枚举
     *
     * @return code
     */
    public static String getCode(AccountTransCode innerEnum) {
        return Optional.ofNullable(innerEnum).map(AccountTransCode::code).orElse(UNKNOWN.code());
    }

    /**
     * map
     *
     * @return Map
     */
    public static Map<String, String> maps() {
        return Stream.of(values()).collect(Collectors.toMap(AccountTransCode::code, AccountTransCode::message));
    }

    /**
     * 判断给定的枚举，是否在列表中
     *
     * @param values 列表
     *
     * @return
     */
    public boolean isInList(AccountTransCode... values) {
        for (AccountTransCode e : values) {
            if (this == e) {
                return true;
            }
        }
        return false;
    }

    /**
     * @return Returns the code.
     */
    @Override
    public String code() {
        return code;
    }

    /**
     * @return Returns the message.
     */
    @Override
    public String message() {
        return message;
    }
}